package com.test.service.impl;

import com.test.dao.UserMapper;
import com.test.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import com.test.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean register(String username, String password) {
    	//1, 查询表里面有没有，有则返回已经有，如果没有，落表返回注册成功；
    	User user = userMapper.selectByPrimaryKey(username);
    	if(user != null){
    		return false;
        } else {
        	user = new User();
        	user.setUsername(username);
        	user.setPassword(password);
        	int insert = userMapper.insert(user);
        	return insert == 1;
        }
    }

    @Override
    public String getAllUsers() {
        List<User> list = userMapper.selectAll();
        String result = "";
        for (User user : list) {
            result += "username:" + user.getUsername() + ", password:" + user.getPassword() + "\r\n";
        }
        return result;
    }
}
